import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:tree_hole/personal/ModifyUserInfo.dart';
import '../LoginPage.dart';
import 'dart:convert';

class PersonalPage extends StatefulWidget{

   String token;
  PersonalPage(this.token);
  _PersonalState createState() => _PersonalState();
}

class _PersonalState extends State<PersonalPage> {

  String _username="这里是用户名";
  String _nickname="还没有昵称哦";
  String _motto="还没有寄语哦";
  int code;
  Response response;

   void initState(){
    super.initState();
    _getUserInfo();
  }

  //获取用户信息
   _getUserInfo(){
     Dio().post("http://121.196.101.7:8080/treehole/user/userInfo",
             options:Options(headers: {
               "token":widget.token
             })).then((value) {
       response=value;
       print(response.data);
       code=response.data["code"];
       if(code==0){
         setState(() {
           this._username=response.data["data"]["username"].toString();
           if(response.data["data"]["nickname"].toString()!=""){
             this._nickname=response.data["data"]["nickname"].toString();
           }
           if(response.data["data"]["motto"].toString()!="null"){
             this._motto=response.data["data"]["motto"].toString();
           }
         });
       }
     });
   }


  Widget build(BuildContext context){
    return new Container(
      decoration: BoxDecoration(
        image: new DecorationImage(
          fit: BoxFit.cover,
          image: new AssetImage(
            'images/paperpage1.jpg'),
        ),
      ),
      child: Column(
        children: [
          Expanded(
            flex: 1,
            child: SizedBox(),),
          Expanded(      //头像，昵称和寄语
              flex: 3,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                      flex:1,
                      child: SizedBox()),
                  Expanded(    //统一头像
                      flex:2,
                      child: Container(
                        child: ClipOval(
                          child: Image.asset(
                            'images/head1.jpg',
                            fit: BoxFit.cover,
                          ),
                        ),
                      )
                  ),
                  SizedBox(width: 10),
                  // Expanded(
                  //     flex: 1,
                  //     child: SizedBox()),
                  Expanded(
                    flex: 5,
                    child: Container(
                      //padding: EdgeInsets.all(12),
                      alignment: Alignment.centerLeft,
                      child: SingleChildScrollView(
                        child: ListTile(
                          title: Text("用户名：${this._username}", textAlign: TextAlign.left,
                                   style: TextStyle(fontSize: 18, color: Colors.black),),
                          subtitle: Text("昵称：${this._nickname}", textAlign: TextAlign.left,
                                   style: TextStyle(fontSize: 18, color: Colors.black),),
                        ),
                        // child: Column(
                        //   children: [
                        //     Text("用户名：${this._username}", textAlign: TextAlign.left,
                        //       style: TextStyle(fontSize: 18, color: Colors.black),),
                        //     Text("昵称：${this._nickname}", textAlign: TextAlign.left,
                        //       style: TextStyle(fontSize: 18, color: Colors.black),),
                        //
                        //   ],
                        // ),
                      ),
                    ),
                    // child:Column(
                    //   children: [
                    //     Expanded(
                    //       flex: 1,
                    //       child: Center(
                    //         child: Text(
                    //           "用户名：${this._username}",
                    //           textAlign: TextAlign.left,
                    //           style: TextStyle(
                    //               fontSize: 18,
                    //               color: Colors.black
                    //           ),
                    //         ),
                    //       ),
                    //     ),
                    //     Expanded(
                    //       flex: 1,
                    //       child: Center(
                    //         child: Text(
                    //           "昵称：${this._nickname}",
                    //           textAlign: TextAlign.left,
                    //           style: TextStyle(
                    //               fontSize: 18,
                    //               color: Colors.black
                    //           ),
                    //         ),
                    //       ),
                    //     ),
                    //     Expanded(
                    //       flex: 1,
                    //       child: Center(
                    //         child: Text(
                    //           "${this._motto}",
                    //           textAlign: TextAlign.left,
                    //           style: TextStyle(
                    //               fontSize: 18,
                    //               color: Colors.black
                    //           ),
                    //         ),
                    //       ),
                    //     )
                    //   ],
                    // ) ,
                  ),
                  Expanded(
                    flex: 1,
                    child: SizedBox(),
                  )
                ],
              )),
          SizedBox(height: 5,),
          Expanded(
            child: Row(
              children: [
                Expanded(child: SizedBox()),
                Expanded(
                  flex: 7,
                  child: Container(
                    child: Text("${this._motto}",
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 18, color: Colors.yellow[800]),
                        overflow: TextOverflow.fade
                    ),
                  ),
                ),
                Expanded(child: SizedBox()),
              ],
            )
          ),
          Expanded(
            flex: 1,
            child: SizedBox(),),
          //三个按钮
          Expanded(
              flex: 2,
              child: Container(
                  width: 280,
                  child:FlatButton(
                    child: Text("通知",style: TextStyle(fontSize: 16,color: Colors.black),),
                    color: Colors.greenAccent[100],
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    ),
                    onPressed: (){
                     print("111");
                    },
                  ))),
          Expanded(
            flex: 1,
            child: SizedBox(),),
          Expanded(
              flex: 2,
              child: Container(
                  width: 280,
                  child:FlatButton(
                    child: Text("修改信息",style: TextStyle(fontSize: 16,color: Colors.black),),
                    color: Colors.greenAccent[100],
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    ),
                    onPressed: () async {
                     String result= await Navigator.push(context, MaterialPageRoute(builder: (context) {
                        return ModifyUserInfo(widget.token);
                      }));
                     if (result=='pop'){
                       setState(() {
                         _getUserInfo();
                       });
                     }
                    },
                  )
              )
          ),
          Expanded(
            flex: 1,
            child: SizedBox(),),
          Expanded(    //注销账号（退出登录）
              flex: 2,
              child: Container(
                  width: 280,
                  child:FlatButton(
                    child: Text("注销账号",style: TextStyle(fontSize: 16,color: Colors.black),),
                    color: Colors.greenAccent[100],
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    ),
                    onPressed: (){
                        Dio().post("http://121.196.101.7:8080/treehole/user/logout",options:Options(headers: {
                          "Token":widget.token
                        })).
                        then((value) {
                            Response response=value;
                            print(response.data);
                            if(response.data['code']==0){
                                Fluttertoast.showToast(
                                  msg: "退出成功",
                                  backgroundColor: Colors.amber[50],
                                  textColor: Colors.black,
                                  fontSize: 15,
                                  toastLength: Toast.LENGTH_SHORT,
                                  gravity: ToastGravity.BOTTOM,);
                                Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context){
                                  return LoginPage();},));
                            }else {
                              Fluttertoast.showToast(
                                msg: response.data["message"],
                                backgroundColor: Colors.amber[50],
                                textColor: Colors.black,
                                fontSize: 15,
                                toastLength: Toast.LENGTH_SHORT,
                                gravity: ToastGravity.BOTTOM,);
                            }
                        });
                    },
                  ))),
          Expanded(
            flex: 2,
            child: SizedBox(),
          )
        ],
      ),
    );
  }
}








